package com.algo.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class InputFileList{
    private List<String> files = new ArrayList<String>();
    private String fromTime, toTime;

    private static final String etlLogDir = "/tong/data/output/dailyMerger";
    private static final String cookieMappingDir = "/user/algo/cookiemapping";
    private static final String cookieMappingTADir = "/user/algo/cookiemappingTA";
    private static final String panelDir = "/user/algo/lujianfeng/panel";
    private static final String plannexDir = "/plannex/text/";

    public static final int etlLog = 0;
    public static final int cookieMapping = 1;
    public static final int cookieMappingTA = 2;
    public static final int panel = 3;
    public static final int plannex = 4;

    public InputFileList(String fromTime, String toTime){
            this.fromTime = fromTime;
            this.toTime = toTime;
    }
    public List<String> getInputFileList(int type){
            if(fromTime.length() != toTime.length())
                    throw new RuntimeException("fromTime is not formated with toTime");
            try{
	            if(fromTime.length() == 6){
	                    SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
	                    Calendar fromCld = Calendar.getInstance();
	                    
	                    	fromCld.setTime(format.parse(fromTime));
	                    	Calendar toCld = Calendar.getInstance();
	                    	toCld.setTime(format.parse(toTime));
		                    while(fromCld.compareTo(toCld) <= 0){
		                            StringBuilder sb = new StringBuilder();
		                            String month = format.format(fromCld.getTime());
		                            switch(type){
		                                    case 0: sb.append(etlLogDir).append("/").append(month).append("*/campaign*"); break;
		                                    case 1: sb.append(cookieMappingDir).append("/").append(month).append("/*"); break;
		                                    case 2: sb.append(cookieMappingTADir).append("/").append(month); break;
		                                    case 3: sb.append(panelDir).append("/l").append(month); break;
		                                    default: throw new RuntimeException("file type is not correct");
		                            }
		                            files.add(sb.toString());
		                            fromCld.add(Calendar.MONTH, 1);
		                    }
	            }
	            else if(fromTime.length() == 8){
	                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
	                    Calendar fromCld = Calendar.getInstance();
	                    fromCld.setTime(format.parse(fromTime));
	                    Calendar toCld = Calendar.getInstance();
	                    toCld.setTime(format.parse(toTime));
	                    while(fromCld.compareTo(toCld) <= 0){
	                        StringBuilder sb = new StringBuilder();
	                        String day = format.format(fromCld.getTime());
	                        switch(type){
	                                case 0: sb.append(etlLogDir).append("/").append(day).append("/campaign*"); break;
	                                case 1: sb.append(cookieMappingDir).append("/").append(day.substring(0, 6)).append("/").append(day); break;
	                                case 2: sb.append(cookieMappingTADir).append("/").append(day.substring(0, 6)); break;
	                                case 3: sb.append(panelDir).append("/l").append(day.substring(0, 6)); break;
	                                case 4: sb.append(plannexDir).append("/").append(day); break;
	                                default: throw new RuntimeException("file type is not correct");
	                        }
	                        files.add(sb.toString());
	                        fromCld.add(Calendar.DATE, 1);
	                }
	        }
	        else    throw new RuntimeException("fromTime string length is too long");
        }catch(ParseException e){
        	throw new RuntimeException(e);
        }
        return files;
    }

	public List<String> getEtlLogInputFileList(){
	        return getInputFileList(etlLog);
	}
	public List<String> getCookieMappingInputFileList(){
	        return getInputFileList(cookieMapping);
	}
	public List<String> getPanelInputFileList(){
	        return getInputFileList(panel);
	}
	public List<String> getCookieTAInputFileList(){
	        return getInputFileList(cookieMappingTA);
	}
	public List<String> getPlannexInputFileList(){
        return getInputFileList(plannex);
	}
	public static void main(String[] args){
	        InputFileList files = new InputFileList("20141120", "20150105");
	        System.out.println(files.getEtlLogInputFileList());
	        System.out.println(files.getCookieMappingInputFileList());
	        System.out.println(files.getPanelInputFileList());
	        System.out.println(files.getCookieTAInputFileList());
	}
}
